package Pet.Management.controller;

import Pet.Management.common.exception.ServiceException;
import Pet.Management.domain.entity.User;
import Pet.Management.service.RegisterService;
import Pet.Management.service.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private CaptchaService captchaService;

    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody RegisterBody registerBody) {
        // 1. 验证验证码
        String code = captchaService.getCaptchaCode(registerBody.getUuid());
        if (code == null) {
            throw new ServiceException("验证码已过期");
        }
        if (!code.equalsIgnoreCase(registerBody.getCode())) {
            throw new ServiceException("验证码错误");
        }
        // 删除验证码
        captchaService.deleteCaptchaCode(registerBody.getUuid());

        // 2. 验证密码是否一致
        if (!registerBody.getPassword().equals(registerBody.getConfirmPassword())) {
            throw new ServiceException("两次输入的密码不一致");
        }

        // 3. 检查用户名是否已存在
        if (registerService.isUserExist(registerBody.getUsername())) {
            throw new ServiceException("用户名已存在");
        }

        // 4. 创建用户
        User user = new User();
        user.setUsername(registerBody.getUsername());
        user.setPassword(registerBody.getPassword());
        user.setStatus((byte) 0);
        
        // 根据注册来源设置管理员权限
        if ("backend".equals(registerBody.getRegisterSource())) {
            user.setIsAdmin((byte) 1);  // 后台注册的用户设置为管理员
        } else {
            user.setIsAdmin((byte) 0);  // 前台注册的用户设置为普通用户
        }
        
        registerService.registerUser(user);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "注册成功");
        return result;
    }
}

class RegisterBody {
    private String username;
    private String password;
    private String confirmPassword;
    private String code;
    private String uuid;
    private Integer isAdmin;
    private String registerSource;

    // Getters and Setters
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Integer getIsAdmin() {
        return isAdmin;
    }

    public void setIsAdmin(Integer isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getRegisterSource() {
        return registerSource;
    }

    public void setRegisterSource(String registerSource) {
        this.registerSource = registerSource;
    }
} 